/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.layerlist;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.Action;
import nl.cloudfarming.client.geoviewer.LayerNode;
import nl.cloudfarming.client.geoviewer.MapController;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.lookup.Lookups;

/**
 * Fasctory for the creation of layer nodes in the layerlist
 *
 * @author Timon Veenstra
 */
public class LayerListChildFactory extends ChildFactory<LayerNode> {

    private final LayerController layerController;

    public LayerListChildFactory(LayerController layerController) {
        this.layerController = layerController;
    }

    public void refresh() {
        refresh(true);
    }

    @Override
    protected boolean createKeys(List<LayerNode> toPopulate) {
        for (Node node : this.layerController.getLayerNodes()) {
            if (node instanceof LayerNode) {
                toPopulate.add((LayerNode) node);
            }
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(final LayerNode key) {
        FilterNode filterNode = new FilterNode(key, null,Lookups.fixed(key.getLayer(), layerController)){

            @Override
            public Action[] getActions(boolean context) {
                List<Action> superActions = new ArrayList();
                if (super.getActions(context) != null && super.getActions(context).length>0){
                    superActions.addAll(Arrays.asList(super.getActions(context)));
                }
                
                superActions.add(new RemoveLayerAction(){

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        layerController.removeLayerNode(key);
                    }
                    
                });
                superActions.add(new SettingsForLayerAction(layerController));
                superActions.add(new VisibilityLayerAction(layerController));
                return superActions.toArray(new Action[superActions.size()]);
            }
            
        };

        return filterNode;
    }

    @Override
    protected Node[] createNodesForKey(LayerNode key) {
        return new Node[]{createNodeForKey(key)};
    }
}
